# Hey Emacs, this is a -*- makefile -*-
#
#   Copyright (C) 2009 Antoine Drouin
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

#
# This is the common Makefile for the stm32-target.
#

SRC_ARCH = arch/stm32

# Pretty Printer
# Call with "make Q=''" to get full command display
Q=@


#
# find compiler toolchain
#
include $(PAPARAZZI_SRC)/conf/Makefile.arm-embedded-toolchain

ifeq ($(ARCH_L),f4)
MCU   = cortex-m4
else
MCU   = cortex-m3
endif
OPT ?= s
# Slightly bigger .elf files but gains the ability to decode macros
DEBUG_FLAGS ?= -ggdb3

CSTANDARD   ?= -std=gnu99
CXXSTANDARD ?= -std=c++0x
CINCS = $(INCLUDES) -I$(PAPARAZZI_SRC)/sw/include

# add syscalls and c++ new/delete operators
$(TARGET).srcs += c++.cpp pprz_syscalls.c
$(TARGET).CXXFLAGS += -fno-sized-deallocation

# input files
SRCS = $($(TARGET).srcs)
#ASRC =

# object files
COBJ    = $(SRCS:%.c=$(OBJDIR)/%.o)
OBJS    = $(COBJ:%.cpp=$(OBJDIR)/%.o)
AOBJ    = $(ASRC:%.S=$(OBJDIR)/%.o)

# linker script :
#   if LDSCRIPT is defined in the airframe use that independantly of TARGET
#   if not, and a TARGET.LDSCRIPT is defined, use that
#   if not, use the default STM32f103re_flash.ld
ifndef LDSCRIPT
ifndef $(TARGET).LDSCRIPT
$(warning Linker script for target "$(TARGET)" on board "$(BOARD)" not defined. Using stm32default.ld.)
LDSCRIPT = $(SRC_ARCH)/stm32default.ld
else
LDSCRIPT = $($(TARGET).LDSCRIPT)
$(info Using "$($(TARGET).LDSCRIPT)" as ldscript for target "$(TARGET)".)
endif
endif

ifeq ($(ARCH_L), )
FLOAT_ABI ?= -msoft-float
else ifeq ($(ARCH_L),f4)
ifndef HARD_FLOAT
FLOAT_ABI ?= -msoft-float
else
FLOAT_ABI ?= -mfloat-abi=softfp -mfpu=fpv4-sp-d16
endif
endif

ARCH_FLAGS ?= -mcpu=$(MCU) -mthumb

# flags for warnings (C and C++)
WARN_FLAGS += -Wall -Wextra -Wunused
#WARN_FLAGS += -Wcast-qual
WARN_FLAGS += -Wcast-align
WARN_FLAGS += -Wpointer-arith
WARN_FLAGS += -Wswitch-default
WARN_FLAGS += -Wredundant-decls -Wmissing-declarations
WARN_FLAGS += -Wshadow


# common flags
CFLAGS += -O$(OPT)
CFLAGS += $(DEBUG_FLAGS)
CFLAGS += $(FLOAT_ABI)
CFLAGS += $(ARCH_FLAGS)
CFLAGS += $(USER_CFLAGS)
CFLAGS += $(WARN_FLAGS)
CXXFLAGS += -O$(OPT)
CXXFLAGS += $(DEBUG_FLAGS)
CXXFLAGS += $(FLOAT_ABI)
CXXFLAGS += $(ARCH_FLAGS)
CXXFLAGS += $(USER_CFLAGS)
CXXFLAGS += $(WARN_FLAGS)
CXXFLAGS += $(CINCS)
CXXFLAGS += $($(TARGET).CFLAGS)
CXXFLAGS += $(BOARD_CFLAGS)


CFLAGS += -I. -I./$(ARCH) -I../ext/libopencm3/include $(INCLUDES)
CFLAGS += -D__thumb2__
CFLAGS += -Wl,--gc-sections
CFLAGS += -mfix-cortex-m3-ldrd
CFLAGS += $(CSTANDARD)
#CFLAGS += -malignment-traps
CFLAGS += -fno-common
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wa,-adhlns=$(OBJDIR)/$(notdir $(subst $(suffix $<),.lst,$<))
CFLAGS += -Wstrict-prototypes -Wmissing-prototypes
CFLAGS += -Wnested-externs
CFLAGS += $($(TARGET).CFLAGS)
CFLAGS += $(BOARD_CFLAGS)

#CFLAGS += -fno-diagnostics-show-caret

ifneq ($(ARCH_L), )
ifeq ($(ARCH_L),f4)
CFLAGS += -DSTM32F4
CXXFLAGS += -DSTM32F4
endif
else
CFLAGS += -DSTM32F1
CXXFLAGS += -DSTM32F1
endif

# with cortex-m3 and m4 unaligned data can still be read
CFLAGS += -DPPRZLINK_UNALIGNED_ACCESS=1


# C++ only flags
CXXFLAGS += $(CXXSTANDARD)
CXXFLAGS += -I. -I./$(ARCH) -I../ext/libopencm3/include $(INCLUDES)
CXXFLAGS += -pipe -fshow-column
CXXFLAGS += -ffunction-sections -fdata-sections
CXXFLAGS += $($(TARGET).CXXFLAGS)


AFLAGS  = -ahls -mapcs-32
AFLAGS += -mcpu=$(MCU) -mthumb
AFLAGS += -x assembler-with-cpp -Wa,-adhlns=$(OBJDIR)/$(<:.S=.lst)

LDFLAGS += -L../ext/libopencm3/lib
LDFLAGS += -T$(LDSCRIPT) -nostartfiles -O$(OPT) -mthumb -mcpu=$(MCU)
LDFLAGS += $(BOARD_LDFLAGS)

ifeq ($(ARCH_L), )
LDFLAGS += -mfix-cortex-m3-ldrd -msoft-float
else ifeq ($(ARCH_L),f4)
ifndef HARD_FLOAT
LDFLAGS += -mfix-cortex-m3-ldrd -msoft-float
else
LDFLAGS += -lnosys -D__thumb2__\
           -mfloat-abi=softfp -mfpu=fpv4-sp-d16
endif
endif

LDFLAGS += -Wl,-Map=$(OBJDIR)/$(TARGET).map,--cref,--gc-sections
ifneq ($(ARCH_L), )
LDLIBS  += -lopencm3_stm32$(ARCH_L)
else
LDLIBS  += -lopencm3_stm32f1
endif
LDLIBS  += -lc -lm -lgcc

CPFLAGS = -j .isr_vector -j .text -j .data
CPFLAGS_BIN = -Obinary
CPFLAGS_HEX = -Oihex

ODFLAGS = -S

# some common informative targets
include $(PAPARAZZI_SRC)/conf/Makefile.arm-embedded-common

# Default target.
all: printcommands sizebefore build sizeafter

# depend order only for parallel make
sizebefore: | printcommands
build: | printcommands sizebefore
sizeafter: | build

build: $(OBJDIR) elf bin hex
# lss sym

$(OBJDIR):
	@echo CREATING object dir $(OBJDIR)
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)

elf: $(OBJDIR)/$(TARGET).elf
bin: $(OBJDIR)/$(TARGET).bin
hex: $(OBJDIR)/$(TARGET).hex
lss: $(OBJDIR)/$(TARGET).lss
sym: $(OBJDIR)/$(TARGET).sym

%.bin: %.elf
	@echo OBJCB $@
	$(Q)$(CP) $(CPFLAGS) $(CPFLAGS_BIN) $< $@

%.hex: %.elf
	@echo OBJCH $@
	$(Q)$(CP) $(CPFLAGS) $(CPFLAGS_HEX) $< $@

# Create extended listing file from ELF output file.
# testing: option -C
%.lss: %.elf
	@echo OBJD $@
	$(Q)$(DMP) -h -S -C $< > $@


# Create a symbol table from ELF output file.
%.sym: %.elf
	@echo NM $@
	$(Q)$(NM) -n $< > $@


# Link: create ELF output file from object files.
.SECONDARY : $(OBJDIR)/$(TARGET).elf
.PRECIOUS : $(OBJS) $(AOBJ)
%.elf: $(OBJS) $(AOBJ) | $(OBJDIR)
	@echo LD $@
	$(Q)$(LD) $(LDFLAGS) $($(TARGET).LDFLAGS) -o $@ $(OBJS) $(AOBJ) $(LDLIBS)

# Compile: create object files from C source files.
$(OBJDIR)/%.o : %.c $(OBJDIR)/../Makefile.ac
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -MMD -c $(CFLAGS) $< -o $@

# Compile: create object files from C++ source files
$(OBJDIR)/%.o : %.cpp $(OBJDIR)/../Makefile.ac
	@echo CXX $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CXX) -MMD -c $(CXXFLAGS) $< -o $@

# Assemble: create object files from assembler source files. ARM/Thumb
$(AOBJ) : $(OBJDIR)/%.o : %.S
	@echo AS $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -c $(AFLAGS) $< -o $@

# Load upload rules
include $(PAPARAZZI_SRC)/conf/Makefile.stm32-upload

# Listing of phony targets.
.PHONY : all build elf bin lss sym

#
# Dependencies
#
ifneq ($(MAKECMDGOALS),clean)
DEPS = $(addprefix $(OBJDIR)/,$($(TARGET).srcs:.c=.d))
DEPS += $(addprefix $(OBJDIR)/,$($(TARGET).srcs:.cpp=.d))
-include $(DEPS)
endif
